package com.etoak.rst.sys.setmeal.service.ServiceImpl;

import com.etoak.rst.sys.dish.DTO.DishDTO;
import com.etoak.rst.sys.setmeal.DTO.SetmealDTO;
import com.etoak.rst.sys.setmeal.entity.Setmeal;
import com.etoak.rst.sys.setmeal.mapper.SetmealMapper;
import com.etoak.rst.sys.setmeal.service.SetmealService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.xml.crypto.Data;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public List<Setmeal> querySome(SetmealDTO dto) {
        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());
        return setmealMapper.querySome(dto);
    }


    /**
     *  删除一堆套餐
     * @param ids
     */
    @Override
    public void deleteIds(List<Integer> ids) {

        setmealMapper.deleteIds(ids);
    }

    @Transactional // spring事务声明式操作 aop + 动态代理(BeanPostProcessor postProcessAfterInitialization// )
    @Override
    public void addSetMealAndDish(SetmealDTO setmealDTO) {

        // 先添加套餐
        setmealMapper.addSetMeal(setmealDTO);
        BigInteger mealId = setmealDTO.getId();
        //再添加套餐菜品
        setmealMapper.addDishes(mealId,setmealDTO.getDishList());
    }

    @Override
    public void updateMealStatus(BigInteger mealId, Setmeal setmeal) {

        setmealMapper.updateMealStatus(mealId,setmeal);
    }

    @Override
    public void updateMeal(SetmealDTO setmealDTO) {

        BigInteger mealId = setmealDTO.getId();

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 修改套餐数据
        setmealDTO.setUpdateTime(dateFormat.format(new Date()));
        setmealMapper.updateMeal(setmealDTO);

        // 删除原有的菜品
        setmealMapper.deletDishById(mealId);
        // 重新添加菜品
        setmealMapper.addDishes(mealId,setmealDTO.getDishList());
    }

}
